package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class MyGenerater {
	private static final Logger LOGGER = Logger.getLogger(MyGenerater.class);

	public static final String PREFIX = "BTM_TEST_";
	public static final String SUFFIX = "_TRG";
	public static List<String> LISTTABLENAMES;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LOGGER.info("Working !!!");

		MyGenerater myGenerater = new MyGenerater();
		try {
			myGenerater.createPurgeStatements();
		}catch (IOException e) {
			LOGGER.error(e.getMessage(), e.getCause());
		}

		MyExecute myExecute = new MyExecute();
		myExecute.myExecuteSQL(myExecute.getLocalConnection());

		LOGGER.info("Finished !!!");
	}

	private void createPurgeStatements() throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/ListTableName.txt")));
		FileWriter fw = new FileWriter(new File("D:/manh.vu/Workspace/CreateTrigger/logs/OutputFile.sql"));
		String tableName = null;
		LISTTABLENAMES = new ArrayList<>();

		final String TRUNCATE = "TRUNCATE TABLE " + PREFIX;
		final String DROP_TABLE = "DROP TABLE " + PREFIX;
		final String DROP_TRIGGER = "DROP TRIGGER " + PREFIX;
		final String COMMA = ";\n";

		while ((tableName = br.readLine()) != null) {
			LISTTABLENAMES.add(tableName);
			fw.write(TRUNCATE);
			fw.write(tableName);
			fw.write(COMMA);
		}
		br.close();
		// Drop trigger
		for(String str : LISTTABLENAMES) {
			fw.write(DROP_TRIGGER);
			fw.write(str + SUFFIX);
			fw.write(COMMA);
		}
		// Drop table
		for(String str : LISTTABLENAMES) {
			fw.write(DROP_TABLE);
			fw.write(str);
			fw.write(COMMA);
		}

		fw.close();
	}
}
